<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="反馈序号" prop="serial">
        <el-input
          v-model="queryParams.serial"
          placeholder="请输入反馈序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="反馈人" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入反馈人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="问题类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择问题类型" clearable>
          <el-option
            v-for="dict in dict.type.problem_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable>
          <el-option
            v-for="dict in dict.type.feedback_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:feedback:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:feedback:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-message"
          size="mini"
          :disabled="single"
          @click="handleHandle"
          v-hasPermi="['system:feedback:edit']"
        >处理</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:feedback:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:feedback:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="反馈序号" align="center" prop="serial" width="120" />
      <el-table-column label="反馈人" align="center" prop="name" width="100" />
      <el-table-column label="联系电话" align="center" prop="phone" width="120">
        <template slot-scope="scope">
          <span>{{ maskPhone(scope.row.phone) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="问题类型" align="center" prop="type" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.problem_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="问题描述" align="center" prop="description" :show-overflow-tooltip="true" />
      <el-table-column label="处理状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.feedback_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="submitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理时间" align="center" prop="handleTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.handleTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:feedback:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-message"
            @click="handleHandle(scope.row)"
            v-hasPermi="['system:feedback:edit']"
          >处理</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:feedback:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改问题反馈对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="反馈人姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入反馈人姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="问题类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择问题类型">
                <el-option
                  v-for="dict in dict.type.problem_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择处理状态">
                <el-option
                  v-for="dict in dict.type.feedback_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="问题描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入问题描述" />
        </el-form-item>
        <el-form-item label="处理反馈" prop="feedback">
          <el-input v-model="form.feedback" type="textarea" placeholder="请输入处理反馈" />
        </el-form-item>
        <el-form-item label="相关图片" prop="images">
          <image-upload v-model="form.images"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 处理反馈问题对话框 -->
    <el-dialog title="处理反馈问题" :visible.sync="handleOpen" width="500px" append-to-body>
      <el-form ref="handleForm" :model="handleForm" :rules="handleRules" label-width="80px">
        <el-form-item label="处理反馈" prop="feedback">
          <el-input v-model="handleForm.feedback" type="textarea" placeholder="请输入处理反馈" />
        </el-form-item>
        <el-form-item label="处理状态" prop="status">
          <el-select v-model="handleForm.status" placeholder="请选择处理状态">
            <el-option
              v-for="dict in dict.type.feedback_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理图片" prop="handleImages">
          <image-upload v-model="handleForm.handleImages" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitHandleForm">确 定</el-button>
        <el-button @click="cancelHandle">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看问题反馈详情对话框 -->
    <el-dialog title="问题反馈详情" :visible.sync="viewOpen" width="800px" append-to-body>
      <div v-if="viewData">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="反馈序号">{{ viewData.serial }}</el-descriptions-item>
          <el-descriptions-item label="反馈人">{{ viewData.name }}</el-descriptions-item>
          <el-descriptions-item label="联系电话">{{ maskPhone(viewData.phone) }}</el-descriptions-item>
          <el-descriptions-item label="问题类型">
            <dict-tag :options="dict.type.problem_type" :value="viewData.type"/>
          </el-descriptions-item>
          <el-descriptions-item label="处理状态">
            <dict-tag :options="dict.type.feedback_status" :value="viewData.status"/>
          </el-descriptions-item>
          <el-descriptions-item label="提交时间">{{ parseTime(viewData.submitTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</el-descriptions-item>
          <el-descriptions-item label="处理时间">{{ parseTime(viewData.handleTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</el-descriptions-item>
          <el-descriptions-item label="处理人">{{ viewData.handleBy }}</el-descriptions-item>
          <el-descriptions-item label="问题描述" :span="2">{{ viewData.description }}</el-descriptions-item>
          <el-descriptions-item label="处理反馈" :span="2">{{ viewData.feedback }}</el-descriptions-item>
          <el-descriptions-item label="相关图片" :span="2" v-if="viewData.images && viewData.images.length > 0">
            <el-image
              v-for="(image, index) in viewData.images"
              :key="index"
              :src="getImageUrl(image)"
              :preview-src-list="getPreviewImages(viewData.images)"
              style="width: 100px; height: 100px; margin-right: 10px;"
              fit="cover"
            >
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
                <div>加载失败</div>
              </div>
            </el-image>
          </el-descriptions-item>
          <el-descriptions-item label="处理图片" :span="2" v-if="viewData.handleImages && viewData.handleImages.length > 0">
            <el-image
              v-for="(image, index) in viewData.handleImages"
              :key="index"
              :src="getImageUrl(image)"
              :preview-src-list="getPreviewImages(viewData.handleImages)"
              style="width: 100px; height: 100px; margin-right: 10px;"
              fit="cover"
            >
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
                <div>加载失败</div>
              </div>
            </el-image>
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFeedback, getFeedback, delFeedback, addFeedback, updateFeedback, handleFeedback } from "@/api/system/feedback";

export default {
  name: "Feedback",
  dicts: ['feedback_status', 'problem_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问题反馈表格数据
      feedbackList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示处理弹出层
      handleOpen: false,
      // 是否显示查看弹出层
      viewOpen: false,
      // 查看数据
      viewData: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serial: null,
        name: null,
        phone: null,
        type: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 处理表单参数
      handleForm: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "反馈人姓名不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "问题类型不能为空", trigger: "change" }
        ],
        description: [
          { required: true, message: "问题描述不能为空", trigger: "blur" }
        ]
      },
      // 处理表单校验
      handleRules: {
        feedback: [
          { required: true, message: "处理反馈不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "处理状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询问题反馈列表 */
    getList() {
      this.loading = true;
      listFeedback(this.queryParams).then(response => {
        this.feedbackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消处理按钮
    cancelHandle() {
      this.handleOpen = false;
      this.resetHandleForm();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serial: null,
        name: null,
        phone: null,
        type: null,
        description: null,
        feedback: null,
        status: "pending",
        images: [],
        submitTime: null,
        handleTime: null,
        handleBy: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    // 处理表单重置
    resetHandleForm() {
      this.handleForm = {
        feedback: null,
        status: "processing",
        handleImages: []
      };
      this.resetForm("handleForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问题反馈";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFeedback(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改问题反馈";
      });
    },
    /** 处理按钮操作 */
    handleHandle(row) {
      this.resetHandleForm();
      const id = row.id || this.ids
      getFeedback(id).then(response => {
        this.handleForm.id = response.data.id;
        this.handleOpen = true;
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.viewData = row;
      this.viewOpen = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFeedback(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFeedback(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交处理表单 */
    submitHandleForm() {
      this.$refs["handleForm"].validate(valid => {
        if (valid) {
          handleFeedback(this.handleForm.id, this.handleForm).then(response => {
            this.$modal.msgSuccess("处理成功");
            this.handleOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除问题反馈编号为"' + ids + '"的数据项？').then(function() {
        return delFeedback(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/feedback/export', {
        ...this.queryParams
      }, `feedback_${new Date().getTime()}.xlsx`)
    },
    /** 手机号脱敏 */
    maskPhone(phone) {
      if (!phone) return '';
      return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
    },
    /** 获取完整的图片URL */
    getImageUrl(image) {
      if (!image) return '';
      // 如果图片URL已经包含完整路径，直接返回
      if (image.startsWith('http') || image.startsWith('//')) {
        return image;
      }
      // 否则加上基础URL
      return process.env.VUE_APP_BASE_API + image;
    },
    /** 获取预览图片列表 */
    getPreviewImages(images) {
      if (!images || images.length === 0) return [];
      return images.map(image => this.getImageUrl(image));
    }
  }
};
</script>

<style scoped>
.image-slot {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 14px;
}

.image-slot i {
  font-size: 30px;
  margin-bottom: 8px;
}
</style>
